package com.he.architecture.entity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.he.architecture.Enums.CustomerOrderTypeEnums;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.Set;

@Entity
@Getter
@Setter
//顾客标志
@DiscriminatorValue("0")
public class Customer extends User{
    @Column
    private String customerName;
    @Column
    private Integer customerAge;
    @Column
    private Boolean customerSex;
    @Column
    private String customerSignature;

    //订单与顾客的多对一关系
    @OneToMany(mappedBy = "customer",
            fetch = FetchType.LAZY,
            orphanRemoval = true,
            cascade = CascadeType.ALL)
    //进行序列化或反序列化时忽略这个特定的属性或方法
    @JsonIgnoreProperties(value = {"customer"})
    private Set<CustomerOrder> customerOrders;


    /**
     * 顾客  形成订单，但是未支付
     */
    public CustomerOrder formOrder(CustomerOrder customerOrder){
        customerOrder.setOrderType(CustomerOrderTypeEnums.NOT_PAYED.getCode());
        return customerOrder;
    }

    /**
     * 顾客  订单状态修改
     */
    public Set<CustomerOrder> orderTypeChang(CustomerOrder customerOrder,CustomerOrderTypeEnums enums){
        Set<CustomerOrder> customerOrders= this.customerOrders ;
        for ( CustomerOrder order:customerOrders) {
            if (order.getId().equals(customerOrder.getId())){
                order.setOrderType(enums.getCode());
                if(CustomerOrderTypeEnums.valueOf(enums.getCode())==CustomerOrderTypeEnums.CANCEL){
                    order.setIsDeleted(1);
                }
            }
        }
        return customerOrders;
    }

}
